package cn.hh.user.service.impl;


import cn.hh.basic.service.impl.BasicServiceImpl;
import cn.hh.user.domain.LoginInfo;
import cn.hh.user.domain.User;
import cn.hh.user.mapper.LoginInfoMapper;
import cn.hh.user.mapper.UserMapper;
import cn.hh.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends BasicServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LoginInfoMapper loginInfoMapper;

    /*添加时要添加到登录信息*/
    @Override
    public void add(User user) {
        LoginInfo loginInfo =  new LoginInfo();
        loginInfo.setUsername(user.getUsername());
        loginInfo.setPhone(user.getPhone());
        loginInfo.setEmail(user.getEmail());
        loginInfo.setSalt(user.getSalt());
        loginInfo.setPassword(user.getPassword());
        loginInfo.setType(1);
        loginInfo.setDisable(1);
        user.setLogininfo_id(loginInfo.getId());
        loginInfoMapper.add(loginInfo);
        userMapper.add(user);
    }
    /*删除用户时要删除登录信息*/
    @Override
    public void deleteById(Long id) {
        /*先查出loginInfo_id*/
        User user = userMapper.loadById(id);
        /*根据loginInfo_id删除user信息*/
        loginInfoMapper.deleteById(user.getLogininfo_id());
        userMapper.deleteById(id);

    }
}
